package com.atguigu.gulimall.order.listener;

import com.atguigu.gulimall.order.entity.OrderEntity;
import com.atguigu.gulimall.order.service.OrderService;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;

/**
 * @DESCRIPTION: 订单关闭的监听器
 * @author: zr
 * @DATE: 2023/5/22 22:18
 */
@Service
@Slf4j
@RabbitListener(queues = "order.release.order.queue")
public class OrderCloseListener {
    @Autowired
    private OrderService orderService;

    @RabbitHandler
    public void listener(OrderEntity entity, Channel channel, Message message) throws IOException {
        log.info("收到过期的消息:准备关闭订单" + entity.getOrderSn());
        try {
            // 关闭订单
            orderService.closeOrder(entity);
            // 监听消息   是否批量提交
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        } catch (Exception e) {
            // 拒绝消息 true 代表将消息重新放到队列中
            channel.basicReject(message.getMessageProperties().getDeliveryTag(), true);
        }

    }

}
